package com.iss.apartmentmanage.controller.admin;
import com.iss.apartmentmanage.service.impl.MyUserDetailService;
import com.iss.apartmentmanage.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("/admin")
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private MyUserDetailService myUserDetailService;

    @ResponseBody
    @RequestMapping(value = "/login-success")
    public String index1()  {

        return getUsername()+"登陆成功";
    }
    @ResponseBody
    @RequestMapping(value = "/r/r2")
    public String r2(){
        return "r2";
    }
    public String getUsername(){
        String username=null;

        //当前认证通过的用户信息,存在上下文中
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //获取用户身份
        Object principal=authentication.getPrincipal();
        System.out.println("principal===="+principal);
        if (principal==null){
            username="匿名";
        }
        if (principal instanceof UserDetails){
            UserDetails userDetails=(UserDetails) principal;
            username=userDetails.getUsername();
        }else {
            username=principal.toString();
        }
        return username;

    }









}
